<?php

/**
* Handles the datatypes being used
*/
abstract class DatatypeBase {
	
	private $column;
	private $default;
	private $unique = false;
	private $allowNULL = false;
	private $zeroFill = false;
	private $unsigned = false;
	private $primary = false;
	private $index = false;
	private $relationship = array();
	
	/* Overwite to use */
	public $length = false;
	public $type   = 'text';
	public $autoInc = false;
	
	function __construct($column, $default, $relationArray = array(), $index = 'n', $length = '', $allowNULL = true ){
		$this->column				= $column;
		$this->relationship = $relationArray;
		$this->default 			= $default;
		$this->allowNULL		= $allowNULL;
		$this->init();
		
		if( !empty($length) ){
			$this->length = $length;
		}
		
		//TODO Add full text
		//TODO Add column type checks so no errors are thrown
		
		switch( $index ){
			case 'p':{ //Primary Key
				$this->primary = true;
				break;
			}
			case 'u':{ //Unique
				$this->unique = true;
				break;
			}
			case 'i':{ //Index
				$this->index = true;
				break;
			}
			default:{ //n is used for null no keys
				
			}
		}
	}
	private function init(){
		$vars['column'] 		= $this->getColumn();
		$vars['default'] 		= $this->getDefault();
		$vars['autoInc'] 		= $this->getIsAutoInc();
		$vars['unique'] 		= $this->getIsUnique();
		$vars['allowNULL'] 	= $this->getIsAllowNull();
		$vars['zeroFill'] 	= $this->getIsZeroFill();
		$vars['unsigned']		= $this->getIsUnsigned();
		$vars['primary']		= $this->getIsPrimary();
		
		if( $this->getIsAutoInc() ){
			$vars['unsigned']	= true;
			$vars['allowNULL'] = false;
		}
		
		foreach($vars as $key=>$val){
			$this->{$key} = $val;
		}
	}
	
	
	public function getType(){
		return $this->type;
	}
	public function getLength(){
		return $this->length;
	}
	public function getColumn(){
		return $this->column;
	}
	public function getRelationship(){
		return $this->relationship;
	}
	public function getDefault(){
		return $this->default;
	}
	public function getIsAutoInc() {
		return $this->autoInc;
	}
	public function getIsPrimary() {
		return $this->primary;
	}
	public function getIsUnique() {
		return $this->unique;
	}
	public function getIsAllowNull() {
		return $this->allowNULL;
	}
	public function getIsZeroFill() {
		return $this->zeroFill;
	}
	public function getIsUnsigned() {
		return $this->unsigned;
	}
	public function getIsIndex(){
		return $this->index;
	}
	
	/* Edit / View Helpers */
	public function view($value, $name, $extra = array()){
		return $value;
	}
	public function edit($value, $name, $extra = array()){
		return $this->input($value, $name, $extra);
	}
	/**
	* Prepare that datatype for insert ( eg MD5 will md5() the value and return new value )
	* @param $value String Value of the insert that you want to prepare
	* @return mixed 
	*/
	public function prepareDatatypeVal($value){
		return $value;
	}
	/**
	* Prepare file for use
	* @param $fileName String file name $_FILES[$fileName] will return file
	* @return mixed 
	*/
	public function prepareDatatypeFile($fileName){
		return $value;
	}
	
	
	
	function post(){
		
	}
	/* Input Default type */
	public function input($value, $name, $extra = array()){
		if( $extra['uneditable'] == true ){
			return $this->inputUneditable($value, $name, $extra);
		}
		$type = isset( $extra['type'] ) ? $extra['type'] : 'text';
		
		$attrArray = array();
		$attrArray[] = 'type="'.$type.'"';
		
		if( $value != "" ) { $attrArray[] = 'value="'.$value.'"'; }
		
		
		switch( $type ){
			case 'file':{
				$extra['class'][] = 'input-file';
				
				//die('ss');
				$attrArray[] = 'name="'.str_replace( array('[',']'), array('_', '') , $name ).'"';

				break;
			}
			default:{
				$attrArray[] = 'name="'.$name.'"';
			}
		}
		
		$attrArray[] = ($extra['disabled'] ? 'disabled="disabled"' : '');;
		$attrArray[] = (is_array( $extra['class'] ) ? 'class="'.implode(' ',$extra['class']).'"' : '');;
		$attrArray[] = ($extra['checked'] ? 'checked="checked"' : '');;

		return '<input '.implode(' ', $attrArray).'/>';
	}
	public function inputWYSIWYG($value, $name, $extra = array()){
		if( $extra['uneditable'] == true ){
			return $this->inputUneditable($value, $name, $extra);
		}
		
		$attrArray = array();
		$attrArray[] = 'type="'.$type.'"';
		$attrArray[] = 'name="'.$name.'"';
		$attrArray[] = ($extra['class'] ? 'class="d-wysiwyg '.$extra['class'].'"' : '');;
		
		return '<textarea class="d-wysiwyg"  '.implode(' ', $attrArray).'>'.$value.'</textarea>';
	}
	public function inputUneditable($value, $name, $extra = array()){
		return '<span class="input-large uneditable-input">'.$value.'</span>';
	}
	public function image($value, $name, $extra = array()){
		if( empty( $value ) ){ 
			return '<img src="http://placehold.it/260x180" alt="" width="260px" height="180px" />';
		}else{
			return 'IMAGE HERE';
		}
	}
}

?>
